package com.godtree.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDao
{
	String url="jdbc:mysql://localhost/jspdemo";
	String userName="root";
	String passWord="hideapple";
	Connection con=null;
	ResultSet rs=null;
	Statement st=null;

	public UserDao()
	{
    	try{
    		Class.forName("com.mysql.jdbc.Driver");
    		System.out.println("数据库连接包加载成功!");
    	}catch(ClassNotFoundException e){
    		System.err.println("con():"+e.getMessage());
    	}
    }
	
	public boolean Insert(String sql)
	{
    	try{
    		con=DriverManager.getConnection(url,userName,passWord);
    		st=con.createStatement();
    		st.executeUpdate(sql);
    	}catch(SQLException e){
    		System.err.println("sql_data.executeInsert:"+e.getMessage());
    		return false;
    	}
    	return true;
    }

    public boolean Delete(String sql)
    {
    	try{
    		con=DriverManager.getConnection(url,userName,passWord);
    		st=con.createStatement();
    		st.executeUpdate(sql);
    	}catch(SQLException e){
    		System.err.println("sql_data.executeDelete:"+e.getMessage());
    		return false;
    	}
    	return true;
    }
    
	public ResultSet Query(String sql)
	{
    	try{
    		con=DriverManager.getConnection(url,userName,passWord);
    		st=con.createStatement();
    		rs=st.executeQuery(sql);
    	}catch(SQLException e){
    		System.err.println("sql_data.executeQuery:"+e.getMessage());
    	}
    	return rs;
    }
	
    public boolean Update(String sql)
    {
    	try{
    		con=DriverManager.getConnection(url,userName,passWord);
    		st=con.createStatement();
    		st.executeUpdate(sql);
    	}catch(SQLException e){
    		System.err.println("sql_data.executeUpdate:"+e.getMessage());
    		return false;
    	}
    	return true;
    }
    
    public void Close1()
    {
    	try{
    		rs.close();
    		st.close();
    		con.close();
    	}catch(SQLException e){
    		e.printStackTrace();
    	}
    }
    
    public void Close2()
    {
    	try{
    		st.close();
    		con.close();
    	}catch(SQLException e){
    		e.printStackTrace();
    	}
    }
  public void createUser(String name,String password) {
	  System.out.println("insert into user(name,password) values('"+name+"','"+password+"')");
		Insert("insert into user(name,password) values('"+name+"','"+password+"')");
	}



}

